package org.opens.entity;

import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.google.common.base.Joiner;
import lombok.Data;

import java.util.Date;

@Data
@TableName(value = "lottery_ticket")
public class LotteryTicket {

    @TableId(value = "id", type = IdType.AUTO)
    private Integer id;

    @TableField(value = "type")
    private String type;

    @TableField(value = "num_of_periods")
    private String numOfPeriods;

    @TableField(value = "red_num1")
    private Integer redNum1;

    @TableField(value = "red_num2")
    private Integer redNum2;

    @TableField(value = "red_num3")
    private Integer redNum3;

    @TableField(value = "red_num4")
    private Integer redNum4;

    @TableField(value = "red_num5")
    private Integer redNum5;

    @TableField(value = "red_num6")
    private Integer redNum6;

    @TableField(value = "blue_num")
    private Integer blueNum;

    @TableField(value = "num_of_times")
    private Integer numOfTimes;

    @TableField(value = "ip_addr")
    private String ipAddr;

    @TableField(value = "create_date")
    private Date createDate;

    /**
     * 验证彩票号码是否合法
     *
     * @return true-合法
     */
    public Boolean isEffective() {
        return (redNum1 != null && redNum1 <= 32 && redNum1 > 0) &&
                (redNum2 != null && redNum2 <= 32 && redNum2 > 0) &&
                (redNum3 != null && redNum3 <= 32 && redNum3 > 0) &&
                (redNum4 != null && redNum4 <= 32 && redNum4 > 0) &&
                (redNum5 != null && redNum5 <= 32 && redNum5 > 0) &&
                (redNum6 != null && redNum6 <= 32 && redNum6 > 0) &&
                (blueNum != null && blueNum <= 16 && blueNum > 0);
    }

    /**
     * 将彩票号码格式化
     *
     * @return 格式化后的彩票号码
     */
    public String getTicketNumber() {
        if (!isEffective()) {
            throw new RuntimeException("错误的彩票号码");
        }
        return Joiner.on(",").join(
                String.format("%02d", redNum1),
                String.format("%02d", redNum2),
                String.format("%02d", redNum3),
                String.format("%02d", redNum4),
                String.format("%02d", redNum5),
                String.format("%02d", redNum6)
        ) + "@" + String.format("%02d", blueNum);
    }

}
